import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-12-29
 * Time:9:58
 */

class MyException extends RuntimeException{
    public MyException(){

    }
    public MyException(String message){
        super(message);
    }
}
class Preson implements Cloneable{
    public String name;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
public class TestDemo {
    public static void func5(){
        int a= 10;
        if (a == 10){
            throw new MyException("异常出错的信息");
        }
    }

    public static void main(String[] args) {
        func5();
    }
    public static void func4() throws RuntimeException,NullPointerException{
        int a = 10;
        if (a == 10){
            throw new RuntimeException("a == 10");
        }
    }
    public static void main7(String[] args) {
        try {
            func4();
        }catch (RuntimeException e){
            e.printStackTrace();
        }
    }
    //方法申明异常自定义的异常
    public static void func3() throws ArrayIndexOutOfBoundsException{
        int[] array = {1,2,3,4};
        System.out.println(array[100]);
    }
    public static void main6(String[] args) {
        try {
            func2();
        }catch (ArrayIndexOutOfBoundsException e){
            e.printStackTrace();
        }

    }
    public static void main5(String[] args) {
//        Scanner scanner = new Scanner(System.in);
//        int n = scanner.nextInt();
//        System.out.println(n);
//        scanner.close();//这个中资源要关闭
        Scanner scanner = new Scanner(System.in);
        try/*(Scanner scanner = new Scanner(System.in))如果try的参数放入了你要关闭的资源，就不用手动关闭了*/ {
            int n = scanner.nextInt();
            System.out.println(n);
        }catch (InputMismatchException e){
            e.printStackTrace();
        }
        finally {
            scanner.close();
        }
    }
    public static int  func2(){
        int[] arr = {1,2,3};
        try {
            return 10;
//            arr = null;
//            System.out.println(arr.length);
        }catch (NullPointerException e){
            e.printStackTrace();
        }finally {
            return 1;
        }

    }

    public static void main4(String[] args) {
        System.out.println(func2());
    }
    public static void main3(String[] args) {
        int[] arr = {1, 2, 3};
        System.out.println("before");
        try {
            arr = null;
            System.out.println(arr[100]);
        }catch (ArrayIndexOutOfBoundsException | NullPointerException e){
            e.printStackTrace();
            System.out.println("数组越界了或者是空指针异常");
        }
//        System.out.println(arr[100]);
        System.out.println("after");
        //代码抛出异常后不会继续执行
        //会抛出异常的位置，方法，类型，原因等
        //发生异常没有处理，程序就会交给JVM来处理，一旦交给JVM来处理程序就会终止。
    }
    //错误
    public static void func(){
        func();
    }

    public static void main2(String[] args) {
        func();
    }
    public static void main1(String[] args) throws CloneNotSupportedException {
        Preson preson = new Preson();
        Preson preson1 = (Preson) preson.clone();
    }
}
